$ ->
  $("#get-sms-code-btn").click(()->
    if( $(this).hasClass('disabled') )

    else
      _mobile = $("#user_mobile").val()

      $.ajax(
        {
          url: '/home/smscode', 
          dataType: 'json', 
          type: 'POST', 
          data: { 'mobile' : _mobile }, 
          complete: ((_response) ->
            if _response.status == 200
            else
              _json = JSON.parse( _response.responseText )
              alert _json.error_msg
          )
        }
      )


      $(this).addClass('disabled')
      count_down_retries()
  )

  $('#reported-reason-selector').change(()->
    $('#reported_reason').val( $(this).val() )
    if $(this).val() == '其它'
      $('#reported-reason-other').removeClass('hidden')
    else if not $('#reported-reason-other').hasClass('hidden')
      $('#reported-reason-other').addClass('hidden')
  )

sms_waiting_seconds = 60
count_down_retries = ()->
  if(sms_waiting_seconds>0)
    $("#get-sms-code-btn").html(sms_waiting_seconds.toString()+"秒后重试")
    sms_waiting_seconds--
    setTimeout(count_down_retries,1000)
  else
    $("#get-sms-code-btn").removeClass("disabled")
    sms_waiting_seconds = 60
    $("#get-sms-code-btn").html("获取验证码")

$(document).ready ->

  $(".yc-admin-right").css "min-height", $(window).height() - $("header").height() - 2

  $(".content").css "min-height", $(window).height() - $("header").height() - $("footer").height() - 59 + "px"

#  $(".index-forms .nav-tabs li").click ->
#    if $(this).index() is 2
#      $(".tab-content .form-group:eq(0) .form-control").attr("value","");
#      $(".tab-content .form-group:eq(0)").removeClass "hidden"
#    else if $(this).index() is 0
#      $(".tab-content .form-group:eq(0) .form-control").attr("value","租车");
#      $(".tab-content .form-group:eq(0)").addClass "hidden"
#    else if $(this).index() is 1
#      $(".tab-content .form-group:eq(0) .form-control").attr("value","信贷");
#      $(".tab-content .form-group:eq(0)").addClass "hidden"

  getFileName = (o) ->
    pos = o.lastIndexOf("\\")
    o.substring pos + 1
  uploadsNum = 0
  $(document).on "click", "#upload-pic1 .upload-div:last .upload-bd", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    $(this).parent(".upload-div").find("input").click()
  $(document).on "click", "#upload-pic2 .upload-div:last .upload-bd", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    $(this).parent(".upload-div").find("input").click()
  $(document).on "click", "#upload-pic3 .upload-div:last .upload-bd", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    $(this).parent(".upload-div").find("input").click()
  $(document).on "click", ".citizen-card-front .upload-bd", ->
    $("#user_picture_citizen_card_front").click()

  $(document).on "change", "#user_picture_citizen_card_front", ->
    $(this).parent(".citizen-card-front").find(".upload-text").text getFileName($(this).val())

  $(document).on "click", ".citizen-card-back .upload-bd", ->
    $("#user_picture_citizen_card_back").click()

  $(document).on "change", "#user_picture_citizen_card_back", ->
    $(this).parent(".citizen-card-back").find(".upload-text").text getFileName($(this).val())

  $(document).on "click", ".business-license .upload-bd", ->
    $("#user_picture_business_license").click()

  $(document).on "change", "#user_picture_business_license", ->
    $(this).parent(".business-license").find(".upload-text").text getFileName($(this).val())

  $(document).on "change", "#upload-pic1 .upload-div input:last", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    uploadsNum += 1
    $(".upload-submit-btn").removeClass "disabled"
    $(this).parent(".upload-div").append "<div class='btn btn-danger btn-delete'>删除</div>"
    $(this).parent(".upload-div").find(".upload-text").text getFileName($(this).val())
    upload_text = "上传被举报人身份证"
    appendHtml = "<div class=\"upload-div\"><span class=\"glyphicon glyphicon-plus\"></span><div class=\"upload-text\">上传被举报人身份证</div><input type=\"file\" name=\"files[]\" class=\"hidden\" /><div class=\"upload-bd\"></div></div>"
    $(this).parents(".col-sm-12").last().append appendHtml
    $(this).parent(".upload-div").last().find("span").remove()

  $(document).on "change", "#upload-pic2 .upload-div input:last", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    uploadsNum += 1
    $(".upload-submit-btn").removeClass "disabled"
    $(this).parent(".upload-div").append "<div class='btn btn-danger btn-delete'>删除</div>"
    $(this).parent(".upload-div").find(".upload-text").text getFileName($(this).val())
    upload_text = "上传被举报人身份证"
    appendHtml = "<div class=\"upload-div\"><span class=\"glyphicon glyphicon-plus\"></span><div class=\"upload-text\">上传合同文件</div><input type=\"file\" name=\"files[]\" class=\"hidden\" /><div class=\"upload-bd\"></div></div>"
    $(this).parents(".col-sm-12").last().append appendHtml
    $(this).parent(".upload-div").last().find("span").remove()

  $(document).on "change", "#upload-pic3 .upload-div input:last", ->
    if $(".upload-div").length is 13
      alert "最多只能上传10张图片！"
      return false
    uploadsNum += 1
    $(".upload-submit-btn").removeClass "disabled"
    $(this).parent(".upload-div").append "<div class='btn btn-danger btn-delete'>删除</div>"
    $(this).parent(".upload-div").find(".upload-text").text getFileName($(this).val())
    upload_text = "上传被举报人身份证"
    appendHtml = "<div class=\"upload-div\"><span class=\"glyphicon glyphicon-plus\"></span><div class=\"upload-text\">上传其他证据</div><input type=\"file\" name=\"files[]\" class=\"hidden\" /><div class=\"upload-bd\"></div></div>"
    $(this).parents(".col-sm-12").last().append appendHtml
    $(this).parent(".upload-div").last().find("span").remove()


  $(document).on "click", ".upload-div .btn-delete", ->
    uploadsNum -= 1
    $(".upload-submit-btn").addClass "disabled"  if uploadsNum is 0
    $(this).parent(".upload-div").remove()

  $(".upload-submit-btn").click (e) ->
    e.preventDefault()
    $(".upload-div:last").find("input").attr "name", "button"
    $(this).parent("form").submit()



$(window).resize ->
  $(".yc-admin-right").css "min-height", $(window).height() - $("header").height() - 2
  $(".content").css "min-height", $(window).height() - $("header").height() - $("footer").height() - 59 + "px"
